import React from 'react'
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import {ActivityIndicator,Tools,List,WhiteSpace,Button,Bubble } from 'weaver-mobile';
import {Route, withRouter } from 'react-router-dom';
import { RouteLayout,Form } from 'weaver-mobile-page';
import AutoCheck from '../reception/AutoCheck';
const getLabel = Tools.getLabel;



@withRouter
@inject ('receptionStore')
@observer
class ToEnterByHand extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            fplx:0,
            sreenHeight:0,
        };

    }

    static defaultProps = {
        prefixCls:"fna-reception-type"
    }

    componentDidMount() {
        const {receptionStore,invoiceResult} = this.props;
        const {getHandSetPage,getEquipmentFn} = receptionStore;

        let params = {
            invoiceResult:invoiceResult,
        }

        getEquipmentFn();
        this.setState({
            sreenHeight:document.documentElement.clientHeight-47
        })

        getHandSetPage(params,(value)=>{
            this.setState({
                fplx:value
            })
        })
        
    }

    componentWillUnmount(){
        const {receptionStore} = this.props;
        const {setInvoiceInfo} = receptionStore;
        setInvoiceInfo();
    }


    render() {
        const { receptionStore,prefixCls,reset_tjfj,invoiceIds } = this.props;
        const { loading_page,ebhAutoCheckRoute,formEnterByHand,invoiceResult,checkJson } = receptionStore;
        const { formParams} = formEnterByHand;

        return (
            <div className={`${prefixCls}`}>
                <ActivityIndicator
                    toast
                    text="Loading..."
                    animating={loading_page}
                />
                {this.getpage()}
            

                {/* 路由层 */}
                <div>
                    <Route
                        key={ebhAutoCheckRoute.id}
                        path={ebhAutoCheckRoute.path}
                        render={() => {
                            return (
                                <RouteLayout getContainer={() => document.getElementById("root")}>
                                    <div
                                        key="crmListLayout"
                                        className="wm-route-layout"
                                    >
                                        
                                        <AutoCheck isForm={true} checkJson={checkJson} invoiceIds={invoiceIds} invoiceResult={invoiceResult} reset_tjfj={reset_tjfj} jump={true}/>
                                    </div>
                                </RouteLayout>
                            )
                        }}
                    /> 
                </div>
                
            </div>
        )
    }

    getpage = (type) =>{
        const {receptionStore} = this.props;
        const {formEnterByHand} = receptionStore;

        let comment = '';
        comment = [
            <div className={'fna-EnterByHand'} key={'EnterByHand'}>
                <h4>{getLabel(517371,'手动输入验票')}</h4>
                <Form store={formEnterByHand} onChange={this.onChange}>
                    <List>
                        {this.getFields()}
                        
                    </List>

                    <WhiteSpace />
                    <div className={'btn-box'}>
                        <Button type="gost" onClick={()=>{this.resetForm()}} >{getLabel(509254,'重置')}</Button>
                        <Button type="primary" onClick={()=>{this.autoCheck()}} >{getLabel(509254,'自动查验')}</Button>
                    </div>
                </Form> 
            </div>
        ]
        

        return comment;
    }

    //重置form
    resetForm = () =>{
        const { receptionStore } = this.props;
        const { formEnterByHand } = receptionStore;
        const { conditioninfo } = formEnterByHand;
    
        // formEnterByHand.resetConditions(conditioninfo, pathname)
        formEnterByHand.resetFields()
    }


    //导入到form表单
    doImport = () =>{
        const {receptionStore,location:{query:{invoiceIds}},history} = this.props;
        const {invoiceInfo} = receptionStore;
        let ids = invoiceIds;

        invoiceInfo.map((items,index) =>{
            if(index == 0 || index == 'undefined' || index == ''){
                ids = items.invoiceIds;
            }else{
                ids += ','+items.invoiceIds;
            }
           
        })

        window.reset_tjfj(ids);
        // console.log('window',window.reset_tjfj)
        history.go(-1)
    }

    getFields = () => {
        const { receptionStore } = this.props;
        const { formEnterByHand } = receptionStore;
        // console.log('form',toJS(formEnterByHand))
        const { conditioninfo, isInit,formParams } = formEnterByHand;

        const groups = [];
        if (isInit) {
            conditioninfo.forEach((group) => {
                const fields = [];
                group.items.forEach((field) => {

                    if(field.domkey[0] === "checkCode"){
                        if(this.state.fplx == 1){
                            fields.push(<Form.Item
                                key={field.domkey.join('__')}
                                form={formEnterByHand}
                                fieldConfig={field}
                                onChange={this.onChange.bind(this)}
                            />) 
                        }
                        
                    }else if(field.domkey[0] === "priceWithoutTax"){
                        if(this.state.fplx == 2){
                            fields.push(<Form.Item
                                key={field.domkey.join('__')}
                                form={formEnterByHand}
                                fieldConfig={field}
                                onChange={this.onChange.bind(this)}
                            />) 
                        }
                    } else{
                        fields.push(<Form.Item
                            key={field.domkey.join('__')}
                            form={formEnterByHand}
                            fieldConfig={field}
                            onChange={this.onChange.bind(this)}
                        />) 
                    }
                    
                });
                
                groups.push(<div key='loading'>{fields}</div>)
            })

            
        }

        return groups;
    }    

    onChange = (paramsObj,value) =>{
        const {receptionStore} = this.props;
        const {formEnterByHand} = receptionStore;

        const domkey = paramsObj.domkey[0];
        if(domkey == "invoiceCode"){
            if(value.length == 10 || value.length == 12){
                let invoiceType = 0;
                if(value.length == 10){
                    let eight = value.substring(7,8);
                    if("1" == eight || "2" == eight || "5" == eight || "7" == eight){
                        invoiceType = 1;
                    }else if("3" == eight || "6" == eight){
                        invoiceType = 2;
                    }
                }else if(value.length == 12){
                    let first = value.substring(0,1);
                    let eight = value.substring(7,8);
                    let elevenTwelve = value.substring(10,11) + value.substring(11,12);
                    if("0" == first){
                        if ("11" == elevenTwelve) {//增值税电子普通发票
                            invoiceType = 15;
                        } else if ("06" == elevenTwelve || "07" == elevenTwelve) {//增值税卷票
                            invoiceType = 16;
                        } else if ("04" == elevenTwelve || "05" == elevenTwelve) {//增值税普通发票
                            invoiceType = 2;
                        } else if ("12" == elevenTwelve) {//通行费发票就是增值税电子发票
                            invoiceType = 3;
                            invoiceType = 15;
                        }
                    }
                    if("1" == first && "2" == eight){//机动车销售统一发票
                        invoiceType = 4;
                    }
                    if("0" == first && "17" == elevenTwelve){//二手车销售统一发票
                        invoiceType = 5;
                    }
                }

                if(invoiceType == 1){
                    this.setState({fplx:2})
                }else if(invoiceType == 2){
                    this.setState({fplx:1})
                }else if(invoiceType == 15 || invoiceType == 16){
                    this.setState({fplx:invoiceType})
                }else if(invoiceType == 4){
                    this.setState({fplx:12})
                }else{
                    this.setState({fplx:0})

                }
            }else{
                this.setState({fplx:0})
            }
        }
        formEnterByHand.updateField(domkey,value);
    }

    autoCheck = () =>{
        const {receptionStore} = this.props;
        const {formEnterByHand} = receptionStore;
        const { formParams} = formEnterByHand;

        if(this.state.fplx == 1 || this.state.fplx == 15 || this.state.fplx == 16){
            formEnterByHand.updateField('priceWithoutTax',1);
            let pass = formEnterByHand.doValidate().pass;
            if(pass){
                this.getInvoiceParams();
            }
        }else if(this.state.fplx == 2 || this.state.fplx == 12){
            formEnterByHand.updateField('checkCode',1);
            let pass = formEnterByHand.doValidate().pass;
            if(pass){
                this.getInvoiceParams();
            }
        }else{
            Bubble.error('失败')
        }

        
        
    }

    //发票参数
    getInvoiceParams = () =>{
        const {receptionStore} = this.props;
        const {getInvoiceInfoByCheck,formEnterByHand,ebhAutoCheckRoute} = receptionStore;
        const { formParams} = formEnterByHand;
        let {invoiceNumber,invoiceCode,billingDate,priceWithoutTax,checkCode} = formParams;
        billingDate = billingDate.replace("-","").replace("-","");
        let result = '';

        if(this.state.fplx == 1 || this.state.fplx == 15 || this.state.fplx == 16){
            result = "01,04,"+invoiceCode+","+invoiceNumber+",0.00,"+billingDate+","+checkCode+",0000,";
        }else if(this.state.fplx == 2 || this.state.fplx == 12){
            result = "01,01,"+invoiceCode+","+invoiceNumber+","+priceWithoutTax+","+billingDate+","+"000000,0000,";
        }

        getInvoiceInfoByCheck({result:result},()=>{
            this.props.history.push(ebhAutoCheckRoute.path)
        })

        return result
    }

    

}

export default ToEnterByHand;